jQuery.fn.exists = function(){return this.length>0;}

var confirmCallback = null;

function objectLength(object) {
	return $.map(object, function(n, i) { return i; }).length;
}

function manageCheckbox(checkbox, value) {
	if (value) {
		checkbox.attr('checked', 'checked');
	}else{
		checkbox.removeAttr('checked');
	}
}

function verticalAlign(elt, ratio, min) {
	if (typeof ratio === 'undefined') {
		ratio = 0.5;
	}
	if (typeof min === 'undefined') {
		min = 20;
	}
	
	var marginTop = Math.max(min, (((elt.parent()).height()  - elt.height()) * ratio) );
	elt.css('margin-top', marginTop + 'px');
}

function _displayPopup(message, title, callback, timer, clazz) {
	if (typeof title === 'undefined') {
		title = 'Erreur';
	}
	if (!$('#overlay').exists()) {
		$('body').append($('<div id="overlay"></div>'));
	}
	if (!$('#mPopup').exists()) {
		$('body').append($('<div id="mPopup" class="popupContainer"><div class="popup"><div class="pTitle title" id="mPopup_title"></div><div class="pMessage message" id="mPopup_message"></div><div class="pActions"><button class="fButton" onclick="triggerPopup()">Fermer</button></div></div></div>'));
	}
	var popupdiv = $('#mPopup .popup');
	popupdiv.removeClass();
	popupdiv.addClass('popup');
	if (!!clazz) {
		popupdiv.addClass(clazz);
	}

	if (typeof callback === 'undefined') {
		confirmCallback = null;
	}else{
		confirmCallback = callback;
	}
	
	$('#mPopup_title').html(title);
	$('#mPopup_message').html(message);
	
	$('body').addClass('mPopupDisplayed');
	$('body').addClass('popupDisplayed');

	verticalAlign($('#mPopup .popup'));
	
	if (typeof timer !== "undefined") {
		$("#mPopup .popup").addClass("timer");
		setTimeout(triggerPopup, timer * 1000);
	} else {
		$("#mPopup .popup").removeClass("timer");
		setTimeout(function() {
			$("#mPopup .fButton").focus();
		});
	}
}

function displayPopup(message, title, callback, timer, clazz) {
	_displayPopup(message, title, callback, timer, clazz);
}
function displayWidePopup(message, title, callback, timer, clazz) {
	_displayPopup(message, title, callback, timer, clazz);
	$('#mPopup').addClass('wide');
}

function displayConfirm(message, callback, title) {
	if (typeof title === 'undefined') {
		title = 'Confirmation';
	}
	
	confirmCallback = callback;
	
	if (!$('#overlay').exists()) {
		$('body').append($('<div id="overlay"></div>'));
	}
	if (!$('#cPopup').exists()) {
		$('body').append($('<div id="cPopup" class="popupContainer"><div class="popup"><div class="pTitle title" id="cPopup_title"></div><div class="cMessage message" id="cPopup_message"></div><div class="pActions"><button class="fButton" onclick="confirmCallback();hidePopup();">Confirmer</button><button onclick="hidePopup();">Annuler</button></div></div></div>'));
	}
	$('#cPopup_title').html(title);
	$('#cPopup_message').html(message);
	
	$('body').addClass('cPopupDisplayed');
	$('body').addClass('cPopupDisplayed popupDisplayed');
	
	var marginTop = ($('#cPopup').height() / 2)  - ($('#cPopup .popup').height() / 2);
	$('#cPopup .popup').css('margin-top', marginTop + 'px');
	
	setTimeout(function(){$('#cPopup .fButton').focus();});
}

function triggerPopup() {
	hidePopup();
	if (confirmCallback != null) {
		confirmCallback();
	}
}
function hidePopup() {
	$('body').removeClass('mPopupDisplayed');
	$('body').removeClass('cPopupDisplayed');
	$('body').removeClass('popupDisplayed');
	$('#mPopup').removeClass('wide');
}
function calculateTempo(val) {
	return (val * 2) + 2;
}
